<?xml version="1.0" ?>
<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
  exclude-result-prefixes="msxsl"
  xmlns:date="http://exslt.org/dates-and-times"
  extension-element-prefixes="date">
<xsl:import href="../Common/date/functions/date-time/date.msxsl.xsl" />
<xsl:output method="xml" />

<xsl:param name="lang" select="'chi'"/>
<xsl:variable name="headers" select="document(/news/header/@href)//header[@lang=$lang]"/>
<xsl:variable name="today"><xsl:value-of select="date:dateTime()" /></xsl:variable>
<xsl:template match = "/">
<rss version="2.0">
  <channel>
    <title><xsl:value-of select="$headers/rsstitle" /></title>
    <link>http://macao.communications.museum/<xsl:value-of select="$lang" />/General/Introduction.html</link>
    <description><xsl:value-of select="$headers/rsstitle" /></description>
    <xsl:call-template name="rsslang" />
    <lastBuildDate><xsl:value-of select="substring(date:dayName(substring($today,1,19)),1,3)" /> <xsl:value-of select="date:formatDate(substring($today,1,19),', d Q yyyy hh:mm:ss z')" /></lastBuildDate>
    <webMaster>webmaster@macao.communications.museum</webMaster>
    <image>
         <title><xsl:value-of select="$headers/cmm" /></title>
         <url>http://macao.communications.museum/images/logo1.jpg</url>
         <link>http://macao.communications.museum</link>
    </image>
    <xsl:apply-templates />
  </channel>
</rss>


</xsl:template>

<xsl:template name="rsslang">
 <xsl:variable name="clang">
  <xsl:choose>
  <xsl:when test="$lang='chi'">zh-tw</xsl:when>
  <xsl:when test="$lang='por'">pt</xsl:when>
  <xsl:when test="$lang='eng'">en-gb</xsl:when>
  <xsl:otherwise>en-gb</xsl:otherwise>
  </xsl:choose>
 </xsl:variable> 
 <language><xsl:value-of select="$clang"/></language>
</xsl:template>

<xsl:template match="news">
    <xsl:apply-templates select="article" />
</xsl:template>

<xsl:template match="article">
  <xsl:variable name="i2">
    <xsl:choose>
       <xsl:when test="string(item[@lang=$lang])!=''">
          <xsl:copy-of select="item[@lang=$lang]/*|item[@lang=$lang]/text()"/>
       </xsl:when>
       <xsl:otherwise>
          <xsl:copy-of select="item[1]/*|item[1]/text()"/>
       </xsl:otherwise> 
    </xsl:choose>
  </xsl:variable>  
  <xsl:variable name="i" select="msxsl:node-set($i2)"/>
  <xsl:variable name="articleTitle">
   <xsl:choose>
      <xsl:when test="$i/a">
         <xsl:copy-of select="$i/a/text()" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$i/p/text()" />
      </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:variable name="articleLinkRel">
   <xsl:choose>
      <xsl:when test="$i/a">
         <xsl:value-of select="$i/a/@href" />
      </xsl:when>
      <xsl:otherwise>http://macao.communications.museum</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:variable name="articleLink">
   <xsl:choose>
    <xsl:when test="substring($articleLinkRel,1,4)!='http'">http://macao.communications.museum<xsl:value-of select="$articleLinkRel" /></xsl:when>
    <xsl:otherwise><xsl:value-of select="$articleLinkRel" /></xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <item>
    <title><xsl:value-of select="$articleTitle" /></title>
    <link><xsl:value-of select="$articleLink" /></link>
    <description><xsl:value-of select="$articleTitle" /></description>
    <xsl:variable name="articleDate">
     <xsl:value-of select="@year"/>-<xsl:value-of select="@month"/>-<xsl:value-of select="@day"/>
    </xsl:variable>
    <pubDate><xsl:value-of select="substring(date:dayName(substring($articleDate,1,10)),1,3)" /> <xsl:value-of select="date:formatDate(substring($articleDate,1,10),', d Q yyyy')" /> 12:00:00 +0800</pubDate>
  </item>
</xsl:template>

</xsl:stylesheet>
